﻿using System.IO;

namespace System
{
	public partial class DijkstraGraph<TP>
	{
		public void Save(String FN = "test.graph")
		{
			TextWriter W = File.CreateText(FN);
			W.WriteLine(Points.Count);
			foreach (DijkstraPoint<TP> p in Points)
				SaveNode(p, W);
			W.WriteLine(Edges.Count);
			foreach (DijkstraEdge<TP> e in Edges)
				SaveEdge(e, W);
			W.Close();
		}
		public void Load(String FN = "test.graph")
		{
			Points.Clear();
			Edges.Clear();
			GC.Collect();

			TextReader R = File.OpenText(FN);
			int NNode = int.Parse(R.ReadLine());
			for (int i = 0; i < NNode; i++)
			{
				DijkstraPoint<TP> node = LoadNode(R);
				node.ID = i;
				Points.Add(node);
			}
			int NLink = int.Parse(R.ReadLine());
			for (int i = 0; i < NLink; i++)
			{
				DijkstraEdge<TP> edge = LoadEdge(R);
				String[] X = R.ReadLine().Split('-');
				var p1 = Points[int.Parse(X[0])];
				var p2 = Points[int.Parse(X[1])];
				Bind(p1, p2, 1);
			}
			R.Close();
		}

		public virtual void SaveNode(DijkstraPoint<TP> P, TextWriter W)
		{
			W.WriteLine(P.Dij_Group);
			if (GetType().GetMethod("SaveNode").DeclaringType == typeof(DijkstraGraph<TP>))
			{
				W.WriteLine("<<Point save not implemented>>");
				throw new NotImplementedException();
			}
		}
		public virtual void SaveEdge(DijkstraEdge<TP> e, TextWriter W)
		{
			W.WriteLine(e.ThisEnd.ID + "-" + e.FarEnd.ID);
			if (GetType().GetMethod("SaveEdge").DeclaringType == typeof(DijkstraGraph<TP>))
			{
				W.WriteLine("<<Point save not implemented>>");
				throw new NotImplementedException();
			}
		}
		public virtual DijkstraPoint<TP> LoadNode(TextReader R)
		{
			throw new NotImplementedException();
		}
		public virtual DijkstraEdge<TP> LoadEdge(TextReader R)
		{
			throw new NotImplementedException();
		}
	}
}